package com.qidian.mes.sys.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.common.util.JSONTools;
import com.qidian.mes.pojo.Dict;
import com.qidian.mes.sys.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.http.HttpResponse;
import java.util.List;

/**
 * @Author 戴着假发的程序员  OKKKK  - CC
 * @Company 起点编程
 * @Description
 * @Time 2024/10/12 上午 10:41
 */
@RestController
@CrossOrigin
@RequestMapping("/dict")
public class DictController {
    @Autowired
    private DictService dictService;
    // 这个方法需要一个HttpServletResponse对象，因为需要一个指向客户端的输出流
    @GetMapping("/export")
    public void export(Dict dict, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit, HttpServletResponse response) throws IOException {
        // 查询数据
        MESResult result = dictService.list(dict,page,limit);
        PageInfo<Dict> pageInfo = (PageInfo<Dict>) result.getData();
        List<Dict> dicts = pageInfo.getList();
        try {
            // 准备输出数据
            // 设置contentType为excel的格式
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("字典列表-" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
            // 让文件以附件的形式下载
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 导出文件 , 这里的第一个参数就是一个输出流，这个流是通过response获取的指向客户端的输出流
            // 这个流不需要你关闭
            EasyExcel.write(response.getOutputStream(),Dict.class)
                    .sheet("字典数据")
                    .doWrite(dicts);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(JSONTools.object2json(MESResult.error("下载文件失败")));
        }
    }

    @GetMapping("/getDict")
    public MESResult getDict(String dictType){
        return dictService.queryDictAndDataByType(dictType);
    };
    @GetMapping("/dicts")
    public MESResult dicts(String [] dictType){
        return dictService.queryDictAndDataByTypes(dictType);
    };

    @PostMapping("/save")
    public MESResult save(@RequestBody  Dict dict){
        return dictService.save(dict);
    };

    @GetMapping("/list")
    public MESResult list(Dict dict, @RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10") int limit){
        return dictService.list(dict,page,limit);
    };
    @GetMapping("/get")
    public MESResult get(Long dictId){
        return dictService.get(dictId);
    };
    @PutMapping("/update")
    public MESResult update(@RequestBody Dict dict){
        return dictService.update(dict);
    };

    @DeleteMapping("/delete")
    public MESResult delete(Long dictId){
        return dictService.delete(dictId);
    };
    @DeleteMapping("/deletes")
    public MESResult deletes(Long [] dictId){
        return dictService.deletes(dictId);
    }
}
